--[[
  兴业银行 - 互金平台 - HTTP
]]
require("apm_core")
require("ApmJson")
require("extension")
local ApmExt = require("ApmExt")

local G_DEBUG = true

if G_DEBUG == false then
    function logDebug(fmt,...) end
    function logInfo(fmt,...) end
    function logWarn(fmt,...) end
    function logError(fmt,...) end
    function logFatal(fmt,...) end
end

local KRED='\x1B[0;31m'
local KSALMON='\x1B[1;31m'
local KGREEN='\x1B[0;32m'
local KLIMEGREEN='\x1B[1;32m'
local KYELLOW='\x1B[0;33m'
local KORANGE='\x1B[0;34m'
local KBLUE='\x1B[1;34m'
local KMAGENTA='\x1B[0;35m'
local KPINK='\x1B[1;35m'
local KCYAN='\x1B[0;36m'
local RESET='\x1B[0m'

local s_flowno
local s_rspcode
local s_rspmsg

local dict_transtype = {}
local dict_channel = {}

local function parseIp(ip)
    local _, _, value = string.find(ip, "(%d+.%d+)$")
    return value == nil and "" or value
end

local function detect_req(s_payload)
    if s_payload == nil then return false end
    local t_json_tree = ApmJson.decode(s_payload)
    return t_json_tree ~= nil, t_json_tree
end

local function detect_resp(s_payload)
    if s_payload == nil then return false end
    local t_json_tree = ApmJson.decode(s_payload)
    return t_json_tree ~= nil, t_json_tree
end

local function parse_req(t_json_tree)
    -- 交易类型
    local s_transtype = t_json_tree.serviceCode
    local s_transtype_name
    if s_transtype ~= nil then
        s_transtype = string.trim(s_transtype)
        s_transtype_name = dict_transtype[s_transtype]
        if s_transtype_name == nil then s_transtype_name = s_transtype end
    end

    -- 交易渠道
    local s_channel = t_json_tree.channelType
    local s_channel_name
    if s_channel ~= nil then
        s_channel = string.trim(s_channel)
        s_channel_name = dict_channel[s_channel]
        if s_channel_name == nil then s_channel_name = s_channel end
    end

    -- 交易流水号
    local s_flowno
    return s_transtype, s_transtype_name, s_channel, s_channel_name, s_flowno
end

local function parse_resp(t_json_tree)
    -- 交易返回码
    local s_rspcode = t_json_tree.responseCode
    -- 交易返回信息
    local s_rspmsg = t_json_tree.errorInfo
    return s_rspcode, s_rspmsg
end

function post_analysis()
    local ApmData = require("ApmData")
    if ApmData.getL7Proto() ~= 80 then return end
    if ApmData.getMsgType() ~= 1 then return end

    --    logInfo("internal_bridge:[%s]", ApmData.dumpFields(true))

    local s_title = ApmData.getField("transaction.title", 1)
    local s_transtype, s_transtype_name, s_channel, s_channel_name, s_flowno
    local s_rspcode, s_rspmsg

    local s_req = ApmData.getField("request.http.body.1", 1)
    if s_req ~= nil then
        s_req = string.trim(s_req)
        local b_ret, t_json_tree = detect_req(s_req)
        if b_ret then
            s_transtype, s_transtype_name, s_channel, s_channel_name, s_flowno = parse_req(t_json_tree)
        end
    end

    local s_resp = ApmData.getField("response.http.body.1", 1)
    if s_resp ~= nil then
        s_resp = string.trim(s_resp)
        s_resp = ApmExt.strToUTF8(s_resp, "GBK")
        ApmData.replaceField("response.http.body.1", s_resp)
        local b_ret, t_json_tree = detect_resp(s_resp)
        if b_ret then
            s_rspcode, s_rspmsg = parse_resp(t_json_tree)
        end
    end

    if s_transtype == nil then
        logInfo("s_transtype is nil title:[%s] REQ:[%s]", s_title, s_req)
    elseif s_rspcode == nil then
        logInfo("s_rspcode is nil RESP:[%s]", s_resp)
    else
        ApmData.replaceField("transaction.type_code", s_transtype)
        ApmData.replaceField("transaction.type_name", s_transtype_name)

        if s_channel ~= nil then
            ApmData.replaceField("transaction.channel_code", s_channel)
            ApmData.replaceField("transaction.channel_name", s_channel_name)
        end

        if s_flowno ~= nil then
            ApmData.replaceField("transaction.code", s_flowno)
        end

        ApmData.replaceField("response.code_string", s_rspcode)
        ApmData.delFields("response.code")

        if s_rspmsg ~= nil then
            ApmData.replaceField("response.error_message", s_rspmsg)
        end

        logInfo("internal_bridge(%s:%d->%s:%d) name:%s|channel:%s|seq:%s|ret:%s|%s",
            ApmData.getSrcIp(),
            ApmData.getSrcPort(),
            parseIp(ApmData.getDstIp()),
            ApmData.getDstPort(),
            s_transtype_name,
            s_channel_name,
            s_flowno,
            s_rspcode,
            s_rspmsg
        )
    end

    return
end
--互金平台 - HTTP
--v1.0.4
